package portada.client;

import java.util.ArrayList;

import portada.client.servicios.ServicioPost;
import portada.client.servicios.ServicioPostAsync;
import portada.client.utils.ServiciosRemotos;
import portada.domain.Post;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.PagingToolbar;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.ToolTip;
import com.gwtext.client.widgets.Viewport;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.event.FieldListenerAdapter;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;

public class Front implements EntryPoint {

	Store	store	= new SimpleStore(new String[] { "comentario", "votosPositivos", "votosNegativos" }, new Object[][] {});
	Panel	panelPrincipal;

	public void onModuleLoad() {

		panelPrincipal = new Panel();
		panelPrincipal.setMonitorResize(true);
		panelPrincipal.add(new HTML("<center>"));
		panelPrincipal.setAutoScroll(true);
		
		
//		Image imagen = new Image();
//		imagen.setUrl("http://www.infla-tu.com/Img/Prod-modelos.jpg");
//		imagen.setWidth("937px");
//		imagen.setHeight("80px");
//		panelPrincipal.add(imagen);
			
		
		panelPrincipal.add(new HTML("<br><br>"));

		ColumnConfig[] columns = new ColumnConfig[] { new ColumnConfig("Descargo", "comentario", 500, true), new ColumnConfig("votosPositivos", "votosPositivos", 100),
				new ColumnConfig("votosNegativos", "votosNegativos", 100)

		};
		ColumnModel columnModel = new ColumnModel(columns);

		GridPanel grid = new GridPanel("grillaPost", 800, 600, store, columnModel);
		
		grid.setFrame(true);
		grid.setStripeRows(true);
		grid.setAutoHeight(true);

		Button b = new Button("Refrescar");

		ButtonListenerAdapter listener = new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				ServicioPostAsync servPostAsync = (ServicioPostAsync) ServiciosRemotos.obtenerInstancia().obtenerServicio(ServicioPost.class);

				AsyncCallback callback = new ElCallback();
				servPostAsync.getPosts(callback);
			}
		};
		b.addListener(listener);
		grid.addButton(b);
		
		/*********/
		
		final PagingToolbar pagingToolbar = new PagingToolbar(store);  
        pagingToolbar.setPageSize(5);  
        pagingToolbar.setDisplayInfo(true);  
        pagingToolbar.setDisplayMsg("Displaying companies {0} - {1} of {2}");  
        pagingToolbar.setEmptyMsg("No records to display");  
  
        NumberField pageSizeField = new NumberField();  
        pageSizeField.setWidth(40);  
        pageSizeField.setSelectOnFocus(true);  
        pageSizeField.addListener(new FieldListenerAdapter() {  
            public void onSpecialKey(Field field, EventObject e) {  
                if (e.getKey() == EventObject.ENTER) {  
                    int pageSize = Integer.parseInt(field.getValueAsString());  
                    pagingToolbar.setPageSize(pageSize);  
                }  
            }  
        });  
  
        ToolTip toolTip = new ToolTip("Enter page size");  
        toolTip.applyTo(pageSizeField);  
  
        pagingToolbar.addField(pageSizeField);  
        grid.setBottomToolbar(pagingToolbar);  
        store.load(0, 5);  
  
		
		
		/************/
		
		panelPrincipal.add(grid);

		panelPrincipal.add(new HTML("<br><br>"));
		panelPrincipal.add(new HTML("<br><br>"));
		
		MultiColumnFormPanel form = new MultiColumnFormPanel();
		panelPrincipal.add(form.getViewPanel());

		
		panelPrincipal.add(new HTML("</center>"));
		Viewport viewport = new Viewport(panelPrincipal);

	}

	// ****************************************************************
	// CLASES CALLBACK
	// ****************************************************************
	private class ElCallback implements AsyncCallback {

		public void onFailure(Throwable caught) {
			System.out.println("error callback");
		}

		public void onSuccess(Object result) {
			store.removeAll();

			ArrayList data = (ArrayList) result;
			int dataSize = data.size();

			RecordDef recDef = new RecordDef(new FieldDef[] { new StringFieldDef("comentario"), new StringFieldDef("votosPositivos"), new StringFieldDef("votosNegativos") });
			Record[] arrayDiRecord = new Record[dataSize];
			for (int i = 0; i < dataSize; i++) {
				Post post = (Post) data.get(i);
				Record rec = recDef.createRecord(new Object[] { "<b>"+ new String(post.getNombre()) + ", " + new String(post.getOcupacion()) + "</b> dice:<br> " + new String(post.getComentario()), "" + post.getVotosPositivos(), "" + post.getVotosNegativos() });
				arrayDiRecord[i] = rec;
			}
			store.insert(0, arrayDiRecord);

			store.commitChanges();
		}
	};

}
